package com.example.controller;

import com.example.interceptor.Auth;
import com.example.interceptor.MvcConfig;
import com.example.model.ClassTableRelation;
import com.example.model.Laboratory;
import com.example.service.*;
import com.example.util.DataMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: tao95
 * Date: 2017/5/8
 * Time: 16:32
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@Controller
@RequestMapping("")
public class IndexController {

    private static final Logger logger = LoggerFactory.getLogger(MvcConfig.class);

    @Autowired
    private UserService userServiceImpl;

    @Autowired
    private CourseService courseServiceImpl;

    @Autowired
    private ClassService classServiceImpl;

    @Autowired
    private LabService labServiceImpl;

    @Autowired
    private TeacherService teacherServiceImpl;

    @Autowired
    private ClassTableRelationService ctrServiceImpl;

    @RequestMapping(value = {"/","/index"})
    public String index(){
        logger.info("index主页面");
        // 直接返回字符串，框架默认会去 spring.view.prefix 目录下的 （index拼接spring.view.suffix）页面
        // 本例为 /WEB-INF/jsp/index.jsp

        return "index";
    }

    @RequestMapping(value = {"/login"})
    public String login(){
        logger.info("登录页面");
        // 直接返回字符串，框架默认会去 spring.view.prefix 目录下的 （index拼接spring.view.suffix）页面
        // 本例为 /WEB-INF/jsp/index.jsp
        return "login";
    }
    @Auth
    @RequestMapping(value = {"/editPass"})
    public String editPass(){
        logger.info("修改密码页面");
        // 直接返回字符串，框架默认会去 spring.view.prefix 目录下的 （index拼接spring.view.suffix）页面
        // 本例为 /WEB-INF/jsp/index.jsp
        return "pass";
    }
    @RequestMapping(value = {"/main"})
    public ModelAndView main(){
        logger.info("显示课程表页面");
        ModelAndView model = new ModelAndView("list");
        ClassTableRelation classTableRelation = new ClassTableRelation();
        Integer laboratoryId = labServiceImpl.findTopLab();
        classTableRelation.setLaboratoryId(laboratoryId);
        classTableRelation.setDaysId(1);
        DataMap dataMap = labServiceImpl.selectLabList();
        model = ctrServiceImpl.findTimeTable(classTableRelation,model);
        model.addObject("labList",dataMap.get("labList"));
        // 直接返回字符串，框架默认会去 spring.view.prefix 目录下的 （index拼接spring.view.suffix）页面
        // 本例为 /WEB-INF/jsp/index.jsp
        return model;
    }
    @Auth
    @RequestMapping(value = {"/addAdmin"})
    public String addAdmin(){
        logger.info("添加管理员页面");
        // 直接返回字符串，框架默认会去 spring.view.prefix 目录下的 （index拼接spring.view.suffix）页面
        // 本例为 /WEB-INF/jsp/index.jsp
        return "addAdmin";
    }
    @Auth
    @RequestMapping(value = {"/adminList"})
    public ModelAndView adminList(){
        logger.info("添加管理员页面");
        ModelAndView modelAndView = new ModelAndView("adminList");
        DataMap dataMap = userServiceImpl.selectAdminList();
        // 直接返回字符串，框架默认会去 spring.view.prefix 目录下的 （index拼接spring.view.suffix）页面
        // 本例为 /WEB-INF/jsp/index.jsp
        modelAndView.addObject("adminList",dataMap.get("adminList"));
        return modelAndView;
    }
    @Auth
    @RequestMapping(value = {"/courseList"})
    public ModelAndView courseList(){
        logger.info("课程管理页面");
        ModelAndView modelAndView = new ModelAndView("courseList");
        DataMap dataMap = courseServiceImpl.selectCourseList();
        // 直接返回字符串，框架默认会去 spring.view.prefix 目录下的 （index拼接spring.view.suffix）页面
        // 本例为 /WEB-INF/jsp/index.jsp
        modelAndView.addObject("courseList",dataMap.get("courseList"));
        return modelAndView;
    }
    @Auth
    @RequestMapping(value = {"/classList"})
    public ModelAndView classList(){
        logger.info("班级管理页面");
        ModelAndView modelAndView = new ModelAndView("classList");
        DataMap dataMap = classServiceImpl.selectClassList();
        // 直接返回字符串，框架默认会去 spring.view.prefix 目录下的 （index拼接spring.view.suffix）页面
        // 本例为 /WEB-INF/jsp/index.jsp
        modelAndView.addObject("classList",dataMap.get("classList"));
        return modelAndView;
    }
    @Auth
    @RequestMapping(value = {"/labList"})
    public ModelAndView labList(){
        logger.info("实验室管理页面");
        ModelAndView modelAndView = new ModelAndView("labList");
        DataMap dataMap = labServiceImpl.selectLabList();
        // 直接返回字符串，框架默认会去 spring.view.prefix 目录下的 （index拼接spring.view.suffix）页面
        // 本例为 /WEB-INF/jsp/index.jsp
        modelAndView.addObject("labList",dataMap.get("labList"));
        return modelAndView;
    }
    @Auth
    @RequestMapping(value = {"/teacherList"})
    public ModelAndView teacherList(){
        logger.info("教师管理页面");
        ModelAndView modelAndView = new ModelAndView("teacherList");
        DataMap dataMap = teacherServiceImpl.selectTeacherList();
        // 直接返回字符串，框架默认会去 spring.view.prefix 目录下的 （index拼接spring.view.suffix）页面
        // 本例为 /WEB-INF/jsp/index.jsp
        modelAndView.addObject("teacherList",dataMap.get("teacherList"));
        return modelAndView;
    }
    @Auth
    @RequestMapping(value = {"/toAddCTR"})
    public ModelAndView toAddCTR(){
        logger.info("课程表添加页面");
        ModelAndView model = new ModelAndView("addCTR");
        model = ctrServiceImpl.getAllData(model);
        return model;
    }
    @Auth
    @RequestMapping(value = {"/toEditCTR"})
    public ModelAndView toEditCTR(Integer id){
        logger.info("课程表修改页面");
        ModelAndView model = new ModelAndView("addCTR");
        model = ctrServiceImpl.getAllData(model);
        ClassTableRelation ctr = ctrServiceImpl.selectById(id);
        model.addObject("ctr",ctr);
        return model;
    }
}